﻿
package system.base;

import java.util.Vector;

import framework.core.*;
import system.data.*;
/*
	Author    :Benny
	CreateTime:2008-05-19 11:09:58
*/
public class BusMaterialAccountSrv extends IHandler
{

	public BusMaterialAccountSrv() 
	{
		super();
	}
	private void deleteBusMaterialAccount(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		BusMaterialAccount obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = BusMaterialAccount.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveBusMaterialAccount(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		BusMaterialAccount obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = BusMaterialAccount.fetch();
    	}
    	else                     //update
    	{
    		obj = BusMaterialAccount.fetch(con,id);
    	}
		BusMaterialAccountPageData data = (BusMaterialAccountPageData)PageData.fromForm(context,BusMaterialAccountPageData.class);
		context.setValue("BusMaterialAccountPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listBusMaterialAccount(DBConnection con,SrvContext context,String path) throws Exception
	{
	    //获得作用于中的event
		BusMaterialAccountSearchEvent event = (BusMaterialAccountSearchEvent)SearchEvent.fromForm(context,BusMaterialAccountSearchEvent.class);
		//设置默认的仓库
		String warehouseCode = "";
		//设置默认的查询时间
		String repairedTime = "";
		//提取是否首次进入
        boolean isfirst = checkFirst(context);
        //首次进入设置默认值
		if(isfirst){
		    event.setWarehouseCode("000000");
		    event.setRepairenTime("000000");
		    warehouseCode = "000000";
		    repairedTime = "000000";
		}else{
		    warehouseCode = context.getString("s_WarehouseCode") == null ? "" : context.getString("s_WarehouseCode");
            repairedTime = context.getString("s_RepairenTime") == null ? "" : context.getString("s_RepairenTime");
		}
		
		//放置event
        context.setSessionValue("BusMaterialAccountSearchEvent",event);
        //放置首次登陆判断结果
        context.setSessionValue("isfirst",Boolean.valueOf(isfirst));
        //获得点击的菜单编码
        String menuCode = context.getString("menu");
        //如果编码空则在session中提取
        if(menuCode == null || menuCode == ""){
            menuCode = (String)context.getSessionValue("choice");
        }else{
            // 为返回请求确定菜单方法
            context.setSessionValue("choice", menuCode);
        }
        // 转换为整形
        int menu = Integer.parseInt(menuCode);
        
        //判断点击菜单
        switch (menu) {
        //各公司分车型单车材料费用
        case 6001:
            if(!isfirst){
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT where WAREHOUSE_CODE = '"+warehouseCode+"' and REPAIREN_TIME like '"+repairedTime+"%'";
                //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountBusiness.getFullData(sql, con);
                if(vec == null || vec.size() <= 0){
                    context.setValue(menuCode, null);
                }else{
                    context.setValue(menuCode, vec);
                }
                //将结果放置request作用域
            }
            //设置跳转
            context.setNextUrl(path+"BusMaterialAccountList6001.jsp");
            break;
        case 6004:
            if(!isfirst){
                Vector vec = BusMaterialAccountBusiness.getResultByYear(repairedTime.substring(0, 4), con);
                if(vec == null || vec.size() <= 0){
                    context.setValue(menuCode, null);
                }else{
                    context.setValue(menuCode, vec);
                }
            }
            //设置跳转
            context.setNextUrl(path+"BusMaterialAccountList6004.jsp");
            break;
        case 6005:
            if(!isfirst){
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT where REPAIREN_TIME like '"+repairedTime+"%'";
                //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountBusiness.getFullData(sql, con);
                //将结果放置request作用域
                context.setValue(menuCode, vec);
            }
            //设置跳转
            context.setNextUrl(path+"BusMaterialAccountList6005.jsp");
            break;
        default:
            break;
        }
	}
	private void backBusMaterialAccount(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BusMaterialAccountList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listBusMaterialAccount"))
		{
			listBusMaterialAccount(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBusMaterialAccount"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccount obj = BusMaterialAccount.fetch(con,id);
			context.setValue("BusMaterialAccount",obj);
			context.setNextUrl(path+"BusMaterialAccountView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBusMaterialAccount"))
		{
			context.setErrorUrl(path+"BusMaterialAccountList.jsp");
			deleteBusMaterialAccount(con,context);
			backBusMaterialAccount(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddBusMaterialAccount"))
		{
			BusMaterialAccount obj = BusMaterialAccount.fetch();
			BusMaterialAccountPageData data = (BusMaterialAccountPageData)PageData.fromTable(obj,BusMaterialAccountPageData.class);
			context.setValue("BusMaterialAccountPageData",data);
			context.setNextUrl(path+"BusMaterialAccountEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBusMaterialAccount"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccount obj = BusMaterialAccount.fetch(con,id);
			BusMaterialAccountPageData data = (BusMaterialAccountPageData)PageData.fromTable(obj,BusMaterialAccountPageData.class);
			context.setValue("BusMaterialAccountPageData",data);
			context.setNextUrl(path+"BusMaterialAccountEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyBusMaterialAccount"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccount obj = BusMaterialAccount.copy(BusMaterialAccount.fetch(con,id));
			BusMaterialAccountPageData data = (BusMaterialAccountPageData)PageData.fromTable(obj,BusMaterialAccountPageData.class);
			context.setValue("BusMaterialAccountPageData",data);
			context.setNextUrl(path+"BusMaterialAccountEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBusMaterialAccount"))
		{
			context.setErrorUrl(path+"BusMaterialAccountEdit.jsp");
			saveBusMaterialAccount(con,context,loginContext);
			backBusMaterialAccount(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBusMaterialAccount"))
		{
			context.setErrorUrl(path+"BusMaterialAccountEdit.jsp");
			saveBusMaterialAccount(con,context,loginContext);
			BusMaterialAccount obj = BusMaterialAccount.fetch();
			BusMaterialAccountPageData data = (BusMaterialAccountPageData)PageData.fromTable(obj,BusMaterialAccountPageData.class);
			context.setValue("BusMaterialAccountPageData",data);
			context.setNextUrl(path+"BusMaterialAccountEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListBusMaterialAccount"))
		{
			backBusMaterialAccount(con,context,path);
			return;
		}
		
		//add by Benny start
		//add by Benny end

		Logger.throwException("BusMaterialAccountSrv","unknown action_code="+action_code);
	}
	
    private boolean checkFirst(SrvContext context) throws Exception{
        boolean isfirst = context.getBoolean("isfirst");
        return isfirst;
    }
}
